<template>
  <page-layout>
    <div slot="toolbar">
      <el-button size="small" round type="primary" @click="newGroup">新增群</el-button>
    </div>
    <div class="card-header">群管理</div>
    <el-card class="box-card">
      <el-card class="box-card">
        <el-form ref="searchGroupCondition" :model="searchGroupCondition" label-width="80px" inline>
          <el-form-item label="群名称">
            <el-input v-model="searchGroupCondition.name" style="width: 300px;"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search(1)">查询</el-button>
          </el-form-item>
        </el-form>
      </el-card>
          <el-card class="box-card">
            <el-table
              :data="groupList.content" border stripe
              style="width: 100%">
              <el-table-column
                prop="id"
                label="编号"
                width="55">
              </el-table-column>
              <el-table-column prop="headimgurl" label="群头像" width="210">
                <template slot-scope="scope">
                  <image-cover :thumb.sync="scope.row.headimgurl" width="187.5px" height="125px" size-type="contain"></image-cover>
                </template>
              </el-table-column>
              <el-table-column prop="name" label="群名称" ></el-table-column>
              <el-table-column label="操作" width="180">
                <template slot-scope="scope">
                  <el-button size="mini" @click="editGroup(scope.$index, scope.row)" type="text">编辑/添加成员</el-button>
                  <el-button size="mini" type="text" @click="deleteGroup(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="mt10">
              <el-pagination
                background
                layout="prev, pager, next"
                :total="groupList.totalElements"
                :page-size="groupList.size"
                :current-page.sync="currentGroupPage" @current-change="getPageChange">
              </el-pagination>
            </div>
          </el-card>
    </el-card>

    <el-dialog :visible.sync="showGroupEditor" title="添加/编辑 群" width="80%">
      <group-editor @close="onGroupEditorClose" :is-close="showGroupEditor"></group-editor>
    </el-dialog>
  </page-layout>
</template>
<script>
import PageLayout from "@/components/page-layout";
import GroupEditor from "./group-editor";
import { mapGetters } from "vuex";
import ImageCover from "@/components/image-cover";
import Status from "@/libs/status";
export default {
  created() {
    this.$store.dispatch("getGroupPageS").then(ret => {});
  },
  mounted() {},
  data() {
    return {
      currentGroupPage: 1,
      showGroupEditor: false,
      showPreview: false,
      curPreObj: {},
      GroupStatus: Status.GroupStatus
    };
  },
  computed: {
    ...mapGetters({
      groupList: "groupList",
      searchGroupCondition: "searchGroupCondition"
    })
  },
  methods: {
    getPageChange(val) {
      var tem = Object.assign(
        { pageIndex: val - 1 },
        this.searchGroupCondition
      );
      this.$store.dispatch("getGroupPageS", tem).then(ret => {});
    },
    search(val) {
      this.getPageChange(1);
    },
    deleteGroup(row) {
      this.$confirm("确认删除当前群?", "提示", {})
        .then(() => {
          this.$store.dispatch("deleteGroupS", { id: row.id }).then(ret => {
            this.getPageChange(1);
            this.$notify({
              title: "删除成功",
              type: "success"
            });
          });
        })
        .catch(() => {});
    },
    preGroup(row) {
      this.$store.dispatch("getGroupS", row.id);
      this.showPreview = true;
    },
    newGroup() {
      this.$store.commit("INIT_CURRENT_GROUP");
      this.showGroupEditor = true;
    },
    editGroup(index, row) {
      this.$store.dispatch("getGroupS", row.id);
      this.showGroupEditor = true;
    },
    onGroupEditorClose() {
      this.showGroupEditor = false;
      this.getPageChange(1);
    },
    onShowPreviewClose() {
      this.showPreview = false;
      this.getPageChange(1);
    }
  },
  components: {
    PageLayout,
    GroupEditor,
    ImageCover
  }
};
</script>
<style>
</style>
